<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="SPU" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="是否新品" prop="is_new">
                    <el-select v-model="queryParams.is_new" placeholder="请选择供应商" clearable style="width: 240px">
                        <el-option v-for="(dict, index) in dict.isNew" :key="index" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="是否有现货" prop="is_current">
                    <el-select v-model="queryParams.is_current" placeholder="请选择产品状态" clearable style="width: 240px">
                        <el-option v-for="(dict, index) in dict.isCurrent" :key="index" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({}, 'add')">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain @click="downloadExcel">下载模板</el-button>
                </el-col>
                <el-col :span="1.5">
                    <upload-button :action="action" @complete="getList"></upload-button>
                </el-col>
                <!-- <el-col :span="1.5">
                  <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
              </el-col> -->
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-tabs v-model="queryParams.check_status" @tab-click="handleQuery" type="border-card">
                <el-tab-pane label="全部" :name="undefined"> </el-tab-pane>
                <el-tab-pane v-for="(item, index) in dict.auditStatus" :key="index" :label="item.label" :name="item.value.toString()"> </el-tab-pane>
            </el-tabs>
            <div class="table-wrap">
            <el-table v-loading="loading" :data="tableData" row-key="id" border script>
                <el-table-column label="ID" prop="id" width="60"></el-table-column>
                <el-table-column label="产品类型" prop="product_attr_type" width="140"></el-table-column>
                <el-table-column label="产品名称" prop="product_name" width="220"></el-table-column>
                <el-table-column label="产品图片" width="100">
                    <template slot-scope="scope">
                        <ImageUploadImg v-model="scope.row.product_images" :readOnly="true"></ImageUploadImg>
                    </template>
                </el-table-column>
                <el-table-column label="产品颜色" prop="color_variant" width="240"></el-table-column>
                <el-table-column label="产品尺码" prop="size_variant" width="160"></el-table-column>
                <el-table-column label="产品成本" prop="purchase_cost" width="120"></el-table-column>
                <el-table-column label="产品售价" prop="sale_cost" width="120"></el-table-column>
                <el-table-column label="产品毛利" prop="gross_profit" width="120"></el-table-column>
                <el-table-column label="产品毛利率%" prop="gross_profit_rate" width="120"></el-table-column>
                <el-table-column label="物流费" prop="shipping_fee" width="120"></el-table-column>
                <el-table-column label="是否新品" width="100">
                    <template slot-scope="scope">
                        {{ scope.row.is_new | formatLabelByValueFuzzy(dict.isNew) }}
                    </template>
                </el-table-column>
                <el-table-column label="是否有现货" width="100">
                    <template slot-scope="scope">
                        {{ scope.row.is_current | formatLabelByValueFuzzy(dict.isCurrent) }}
                    </template>
                </el-table-column>
                <el-table-column label="审核状态" width="120">
                    <template slot-scope="scope">
                        {{ scope.row.check_status | formatLabelByValueFuzzy(dict.auditStatus) }}
                    </template>
                </el-table-column>
                <el-table-column label="产品毛量" prop="base_weight" width="120"></el-table-column>
                <el-table-column label="供应商" prop="supply_links" width="300">
                    <template slot-scope="scope">
                        <el-tooltip effect="dark" :content="scope.row.supply_link" placement="top">
                            <el-link type="primary" @click="toSupply(scope.row.supply_link)">{{ scope.row.supply_name }}</el-link>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="供应商" prop="supply_links" width="300" class-name="border-cell">
                  <template slot-scope="scope" v-if="scope.row.supply_links.length > 0">
                      <div v-for="(item, index) in scope.row.supply_links" :key="index" class="border-cell-item">
                          <el-tooltip effect="dark" :content="item.supply_link" placement="top">
                              <el-link type="primary" @click="toSupply(item.supply_link)">{{ item.supply_name }}</el-link>
                          </el-tooltip>
                      </div>
                  </template>
              </el-table-column> -->
                <el-table-column label="备注" prop="remarks" min-width="160"></el-table-column>
                <el-table-column label="运营者" prop="develper" width="120"></el-table-column>
                <el-table-column label="创建时间" prop="create_time" width="160"></el-table-column>
                <el-table-column label="修改时间" prop="update_time" width="160"></el-table-column>
                <el-table-column label="操作" width="220" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-search" @click="showDetailDialog(scope.row, 'view')">详情</el-button>
                        <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row, 'edit')">编辑</el-button>
                        <el-button type="text" icon="el-icon-document" @click="showProductNewAuditDialog(scope.row, 'edit')" v-hasPermi="'/product/ProductNew/check'">审核</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
        </div>
        <product-new-detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></product-new-detail-dialog>
        <product-new-audit-dialog v-model="auditDialog.visible" v-if="auditDialog.visible" :params="auditDialog.data" @success="handleQuery"></product-new-audit-dialog>
    </div>
</template>
<script>
import { queryProductNewList } from '@/api/product'
import ProductNewDetailDialog from '../components/ProductNewDetailDialog.vue'
import ProductNewAuditDialog from '../components/ProductNewAuditDialog.vue'
import { exportFile } from '@/utils/index'
import { parseTime } from '@/utils/quickadmin'
import { AUDIT_STATUS, IS_NEW_TYPE, IS_CURRENT_TYPE } from '../constant'
export default {
    name: 'ProductNewList',
    components: { ProductNewDetailDialog, ProductNewAuditDialog },
    data() {
        return {
            action: process.env.VUE_APP_BASE_API + '/product/ProductNew/importProduct',
            downloadUrl: process.env.VUE_APP_BASE_API + '/storage/system/product_new.xlsx',
            // 遮罩层
            loading: true,
            download: false,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 状态数据字典

            dict: {
                auditStatus: AUDIT_STATUS,
                isNew: IS_NEW_TYPE,
                isCurrent: IS_CURRENT_TYPE,
                check_status: undefined
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                spu: undefined,
                is_new: undefined,
                is_current: undefined,
                create_time: [],
            },
            detailDialog: {
                visible: false,
                data: {},
                type: '',
            },
            auditDialog: {
                visible: false,
                data: {},
            },
        }
    },
    async created() {
        this.loading = true
        try {
            const attrData = await getAttrList()
            this.dict.attrList = attrData?.data ?? []
            const purchaseLevelData = await getPurchaseLevel()
            this.dict.purchaseTypeList = purchaseLevelData?.data ?? []
            const supplyData = await getSupplyList()
            this.dict.supplyList = supplyData?.data ?? []
        } catch (error) {
            this.loading = false
        }
        this.getList()
    },
    methods: {
        toSupply(url) {
            window.open(url, '_blank')
        },
        handleClick() {
            this.getList()
        },
        downloadExcel() {
            window.open(this.downloadUrl, '_blank')
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            exportProduct(qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        showProductNewAuditDialog(row) {
            this.auditDialog.visible = true
            this.auditDialog.data = row
        },

        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            queryProductNewList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style scoped>
.border-cell {
    padding: 0 !important;
}
.border-cell .cell {
    padding: 0 !important;
}
.border-cell-item {
    padding: 10px;
    border-bottom: 1px solid #dfe6ec;
}
.border-cell-item:last-child {
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
